<?php

namespace app\common\service;


use app\model\FreshUserAward;
use app\util\Tools;
use think\Cache;
use think\Db;

class AgentSettle{



    /**
     * 佣金记录生成
     * @param $type 1订单 2充值
     * @param $order
     * @return bool
     */
    public static function settleSubmit($type, $order){
        if($order['profitTotal'] == 0) return false;
        $programModel = new \app\model\UserProgram();
        $user_program = $programModel->where('userID',$order['userID'])->find();
        if($user_program['freshAgentID']){   //个人代理代理商奖励
            $agent = Db::name('fresh_agent')->where('agentID', $user_program['freshAgentID'])->find();
            if($agent['status'] != 1) return false;
            //直推奖
            self::referReward($agent, $order, $type);
        }else{
            return  false;
        }
        return true;
    }

    //发放直推奖
    public static function referReward($agent, $order, $type){
        if($type ==1){
            $radio = config('agent.order');
        }elseif ($type ==2){
            $radio = config('agent.recharge');
        }else{
            $type = 3;
            $radio = config('agent.vip');
        }
        if($radio > 0){
            $money = Tools::bcCalculate($order['profitTotal'], $radio, '*');
            $remark = "获得{$money}元推荐奖";
            $areaID = $agent['areaID']?$agent['areaID']:'';
            self::insertReward($agent['agentID'], $radio, $money, $order['userID'], $order['orderID'], $type,$remark, $areaID);
        }
    }


    public static function insertReward($agentID, $radio, $money, $sourceUserID, $orderID,$type, $remark,$areaID){

        $add = [
            'agentID' => $agentID,
            'money' => $money*100,
            'radio' => $radio,
            'type' => $type,
            'status' => 2,
            'sourceUserID' => $sourceUserID,
            'relateID' => $orderID,
            'addTime' => time(),
            'addDate' => date('Ymd'),
            'payDate' => date('Ymd'),
            'month' => date('Ym'),
            'areaID' => $areaID,
            'remark' => $remark,
        ];
//        $flag = FreshUserAward::create($add);
        $flag = Db::name('fresh_agent_award')->insertGetId($add);
        if(!$flag) throw new \Exception("佣金结算失败-1");
//        self::changeMoney($agentID, $money, $remark, $flag); //发放奖励
    }

    /**
     * 佣金发放
     * @param $userID
     * @param $money
     * @param $remark
     * @param $userAwardId
     * @throws \think\exception\DbException
     */
    public static function changeMoney($agentID, $money, $remark, $userAwardId){
        $agentModel = \app\model\FreshAgent::get(['agentID' => $agentID]);
        $freshMoney = $agentModel->money + $money;
        $agentModel->money = $freshMoney;
        $agentModel->moneyTotal = $agentModel->moneyTotal+$money;
        $resAgent = $agentModel->save();
        //添加记录进流水表
        $moneylog = [
            "agentID" => $agentID,
            'ordertype' => 1,
            "amount" => $money,
            "turnover" => 1,
            "content" => $remark,
            "addtime" => time(),
            "balance" => $freshMoney,
            "relateID" => $userAwardId,
        ];
        $resMoneyLog = \app\model\FreshAgentMoneyLog::create($moneylog);
        if(!$resMoneyLog || !$resAgent){
            throw new \Exception("佣金结算失败-2");
        }
    }




}


?>
